package com.eightywork.life.activity;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.service.CacheService;
import com.umeng.analytics.MobclickAgent;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class GuideActivity extends BaseActivity implements OnClickListener {
	private ViewPager viewPager;
	private List<ImageView> pageViews;
	private ImageButton searchButton, cancleButton;
	private RelativeLayout canclerl;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_guide);
		initView();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}


	private void initView() {
		pageViews = new ArrayList<ImageView>();
		for (int i = 1; i <= 5; i++) {
			ImageView pic = new ImageView(this);
			int resid = getResources().getIdentifier("guiding_fw" + i,
					"drawable", this.getPackageName());
			pic.setImageResource(resid);
			pageViews.add(pic);
		}
		viewPager = (ViewPager) findViewById(R.id.awesomepager);
		for (int i = 0; i < pageViews.size(); i++) {
			viewPager.setAdapter(new PagerAdapter() {

				@Override
				public boolean isViewFromObject(View arg0, Object arg1) {
					return arg0 == arg1;
				}

				@Override
				public int getCount() {
					return pageViews.size();
				}

				@Override
				public int getItemPosition(Object object) {
					return super.getItemPosition(object);
				}

				@Override
				public void destroyItem(View arg0, int arg1, Object arg2) {
					((ViewPager) arg0).removeView(pageViews.get(arg1));
				}

				@Override
				public Object instantiateItem(View arg0, int arg1) {
					ImageView pic = pageViews.get(arg1);
					((ViewPager) arg0).addView(pic);
					return pageViews.get(arg1);
				}
			});

		}

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				Log.d("position", position + "");
				if (position == 4) {
					searchButton.setVisibility(View.VISIBLE);
				} else {
					searchButton.setVisibility(View.GONE);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		searchButton = (ImageButton) findViewById(R.id.imageButton1);
		searchButton.setVisibility(View.GONE);
		searchButton.setOnClickListener(this);
		cancleButton = (ImageButton) findViewById(R.id.guide_cancle_btn);
		cancleButton.setOnClickListener(this);
		canclerl = (RelativeLayout) findViewById(R.id.guide_cancle_rl);
		canclerl.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v == searchButton) {
			if (CacheService.isRookie()) {
				Intent intent = new Intent(context, LoginMapActivity.class);
				startActivity(intent);
				CacheService.iamSuperman();
			}
			finish();
		}
		if(v == cancleButton || v == canclerl){
		//	cancleButton.setImageDrawable(getResources().getDrawable(R.drawable.wrong_1));
			if (CacheService.isRookie()) {
				Intent intent = new Intent(context, LoginMapActivity.class);
				startActivity(intent);
				CacheService.iamSuperman();
			}
			finish();
		}
	}
}
